<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/6/21
 * Time: 11:18
 */

namespace app\project\controller;


use app\common\controller\Common;
use app\project\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Strategy extends Controller
{

    //惠企政策首页
    public function index(){
        //权限查询
        $common = new Common();

        if(!empty($_GET['keyword'])){
            $where['title'] = ['like',"%{$_GET['keyword']}%"];
        }
        if(!empty($_GET['department'])){
            $where['department'] = $_GET['department'];
        }
        if(!empty($_GET['scale'])){
            $where['scale'] = $_GET['scale'];
        }
        if(!empty($_GET['industry'])){
            $where['industry'] = $_GET['industry'];
        }

        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['pageSize']) ? 10 : $_GET['pageSize'];
                $where['id'] = ['gt',0];
                $returnBlack = PublicService::ListService("project_strategy",$where,null,$page,$limit);
                $list = $returnBlack['list'];
                foreach ($list as $k=>$v){
                    $list[$k]['startTime'] = empty($v['startTime'])?"长期":$v['startTime'];
                    $list[$k]['endTime'] = empty($v['endTime'])?"长期":$v['endTime'];
                    //查询部门名称
                    $department = $common->public_select("project_type",['id'=>$v['department']])->value("name");
                    $list[$k]['department'] = empty($department)?"":$department;
                }
                $count = $returnBlack['count'];
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }else{
            //查询发布部门
            unset($where);
            $where['type'] = 9;
            $assign['departmentList'] = $departmentList = PublicService::ListService("project_type",$where);

            //查询企业规模
            $where['type'] = 10;
            $assign['scaleList'] = $scaleList = PublicService::ListService("project_type",$where);

            //查询行业
            $where['type'] = 11;
            $assign['industryList'] = $industryList = PublicService::ListService("project_type",$where);

            $this->assign($assign);
            return $this->fetch();
        }
    }

    //惠企政策添加视图
    public function detail(){
        $id = empty($_GET['id'])?0:$_GET['id'];
        $where['id'] = $id;
        $detail = PublicService::detailService("project_strategy",$where);
        $assign['detail'] =  $detail;

        //查询发布部门
        unset($where);
        $where['type'] = 9;
        $assign['departmentList'] = $departmentList = PublicService::ListService("project_type",$where);

        //查询企业规模
        $where['type'] = 10;
        $assign['scaleList'] = $scaleList = PublicService::ListService("project_type",$where);

        //查询行业
        $where['type'] = 11;
        $assign['industryList'] = $industryList = PublicService::ListService("project_type",$where);

        //获取企业账号
        $common = new Common();
        $thisWechat = Session::get("thisWechat");
        $accountWhere['uniacid'] = $thisWechat['id'];
//        $accountWhere['role'] = 1;
        $account = $common->public_select("project_account", $accountWhere, "id value,name title")->select();
        $assign['account'] = json_encode($account);

        $business_v = explode(";", trim($detail['business'], ";"));
        $account_k = $this->takekey($account, $business_v);

        $assign['account_k'] = json_encode($account_k);
        $this->assign($assign);
        return view();
    }

    //添加操作
    public function save(){
        try{
            $common = new Common();
            $rule = [
                ["title", "require", "名称不能为空"],
                ["path", "require", "文件不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $id = PublicService::saveService("project_strategy",$data,0);
            $log['remark'] = "账号【" . Session::get("user_name") . "】添加惠企政策【".$data['title']."】id【".$id."】";

            //短信通知操作
            $business = array_unique(array_filter(explode(";",$data['business'])));
            foreach ($business as $item){
                $phone = Db::name("project_account")->where(['name'=>$item])->value("phone");
                if(!empty($phone)){
                    //通知用户
                    $sina = "湖北东土太一智慧科技";
                    sms($phone, $sina, "SMS_488870197", "");
                }
            }
            //短信通知操作end


            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    public function takekey($data, $value)
    {
        $black = [];
        foreach ($data as $k => $v) {
            if (in_array($v['title'], $value)) {
                $black[] = $v['value'];
            }
        }
        return $black;
    }

    //删除操作
    public function del(){
        try{
            $id = $_POST['id'];
            $where['id'] = ['in',$id];
            PublicService::delService("project_strategy",$where);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}